#include "elk_resource_view.h"
#include "ui_elk_resource_view.h"
#include "elk_common_dlg.h"

ElkResourceView::ElkResourceView(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ElkResourceView),
    model_(new QStandardItemModel(this))
{
    ui->setupUi(this);
    ui->tableView->setModel(model_);
}

ElkResourceView::~ElkResourceView()
{
    delete ui;
}

void ElkResourceView::bind(QWeakPointer<ElkWebDelegate> delegate)
{
    if (!delegate_) {
        delegate_ = delegate;

        connect(delegate_.data(), SIGNAL(beforeBrowser(const QString &,
                                                       const ElkRequestDetails &,
                                                       bool, bool)),
                this, SLOT(onBeforeBrowser(const QString &,
                                           const ElkRequestDetails &,
                                           bool, bool)));

        connect(delegate_.data(), SIGNAL(resourceRequested(const QString &,
                                                           const ElkRequestDetails &)),
                this, SLOT(onResourceRequested(const QString &,
                                               const ElkRequestDetails &)));
    }
}

void ElkResourceView::request(const QString &frame, const ElkRequestDetails &details)
{
    QStandardItem *item;
    QList<QStandardItem*> row;

    if (frame.isEmpty()) {
        item = new QStandardItem(QString("Main Frame"));
    } else {
        item = new QStandardItem(frame);
    }
    item->setTextAlignment(Qt::AlignCenter);
    row.append(item);

    item = new QStandardItem(details.resourceType);
    item->setTextAlignment(Qt::AlignCenter);
    row.append(item);

    item = new QStandardItem(details.method);
    item->setTextAlignment(Qt::AlignCenter);
    row.append(item);

    item = new QStandardItem(details.url);
    row.append(item);

    bool hide = false;
    if (!resourceTypeSelected_.isEmpty()) {
        hide = !resourceTypeSelected_.contains(details.resourceType, Qt::CaseInsensitive);
    }

    model_->appendRow(row);
    ui->tableView->setRowHidden(model_->rowCount() - 1, hide);

    requests_.append(details);
}

void ElkResourceView::onBeforeBrowser(const QString &frame,
                                      const ElkRequestDetails &details,
                                      bool userGesture, bool isRedirect)
{
    Q_UNUSED(details);
    if (frame.isEmpty()) {
        if (userGesture || !isRedirect) {
            on_pushButtonClear_clicked();
        }
    }
}

void ElkResourceView::onResourceRequested(const QString &frame, const ElkRequestDetails &details)
{
    request(frame, details);
}

void ElkResourceView::on_pushButtonClear_clicked()
{
    model_->clear();
    requests_.clear();
}

void ElkResourceView::on_pushButtonDownload_clicked()
{
    auto index = ui->tableView->currentIndex();
    if (index.isValid()) {
        emit delegate_.data()->downloadRequested(requests_[index.row()].url, QString());
    }
}

void ElkResourceView::on_tableView_doubleClicked(const QModelIndex &index)
{
    if (index.isValid()) {
        QStringList list;
        auto frame = model_->item(index.row(), 0);
        list.append(QString("request flags: ") + requests_[index.row()].requestFlags);
        list.append(QString("transition type: ") + requests_[index.row()].transitionType);
        list.append(QString());
        list.append(requests_[index.row()].url);
        ElkCommonDlg::showTextBrowserDlg(this->window(), frame->text(), list);
    }
}

void ElkResourceView::on_radioButtonAll_toggled(bool checked)
{
    if (checked) {
        ui->checkBoxXhr->setChecked(false);
        ui->checkBoxScript->setChecked(false);
        ui->checkBoxStylesheet->setChecked(false);
        ui->checkBoxImage->setChecked(false);
        ui->checkBoxMedia->setChecked(false);
        resourceTypeSelected_.clear();
    }
    updateResourceTypeSelection();
}

void ElkResourceView::on_checkBoxXhr_toggled(bool checked)
{
    if (checked) {
        ui->radioButtonAll->setChecked(false);
        resourceTypeSelected_.append("[xhr]");
    } else {
        resourceTypeSelected_.remove("[xhr]");
    }
    updateResourceTypeSelection();
}

void ElkResourceView::on_checkBoxScript_toggled(bool checked)
{
    if (checked) {
        ui->radioButtonAll->setChecked(false);
        resourceTypeSelected_.append("[script]");
    } else {
        resourceTypeSelected_.remove("[script]");
    }
    updateResourceTypeSelection();
}

void ElkResourceView::on_checkBoxStylesheet_toggled(bool checked)
{
    if (checked) {
        ui->radioButtonAll->setChecked(false);
        resourceTypeSelected_.append("[css]");
    } else {
        resourceTypeSelected_.remove("[css]");
    }
    updateResourceTypeSelection();
}

void ElkResourceView::on_checkBoxImage_toggled(bool checked)
{
    if (checked) {
        ui->radioButtonAll->setChecked(false);
        resourceTypeSelected_.append("[image]");
    } else {
        resourceTypeSelected_.remove("[image]");
    }
    updateResourceTypeSelection();
}

void ElkResourceView::on_checkBoxMedia_toggled(bool checked)
{
    if (checked) {
        ui->radioButtonAll->setChecked(false);
        resourceTypeSelected_.append("[media]");
    } else {
        resourceTypeSelected_.remove("[media]");
    }
    updateResourceTypeSelection();
}

void ElkResourceView::updateResourceTypeSelection()
{
    for (int i = 0; i < requests_.size(); i++) {
        bool hide = false;
        if (!resourceTypeSelected_.isEmpty()) {
            hide = !resourceTypeSelected_.contains(requests_[i].resourceType, Qt::CaseInsensitive);
        }
        ui->tableView->setRowHidden(i, hide);
    }
}
